<template>
  <div class="login-container">
    <h2>登录</h2>
    <form @submit.prevent="handleLogin">
      <div class="form-group">
        <label>用户名：</label>
        <input v-model="username" required placeholder="请输入用户名" />
      </div>
      <div class="form-group">
        <label>密码：</label>
        <input v-model="password" type="password" required placeholder="请输入密码" />
      </div>
      <div v-if="errorMsg" style="color:red;margin-bottom:10px;">{{ errorMsg }}</div>
      <button type="submit">登录</button>
    </form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useAuthStore } from '../stores/auth'
import { useRouter } from 'vue-router'

const username = ref('')
const password = ref('')
const errorMsg = ref('')
const auth = useAuthStore()
const router = useRouter()

async function handleLogin() {
  errorMsg.value = ''
  try {
    await auth.login(username.value, password.value)
    router.push('/')
  } catch (e) {
    errorMsg.value = e.message || '登录失败，请检查用户名和密码'
  }
}
</script>

<style scoped>
.login-container {
  max-width: 350px;
  margin: 80px auto;
  padding: 32px 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px #0001;
  background: #fff;
}
.form-group {
  margin-bottom: 18px;
}
input, select {
  width: 100%;
  padding: 8px 6px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
button {
  width: 100%;
  padding: 10px;
  background: #42b983;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}
button:hover {
  background: #369870;
}
</style> 